﻿using Common.Interfaces;
using Microsoft.AspNetCore.Http;
using ModelDto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Achieve
{
    public class UserInfoHelper : IUserInfoHelper
    {
        private readonly IHttpContextAccessor httpContextAccessor;
        private readonly IJwtHelperAppService _jwtHelperAppService;
        public UserInfoHelper(IHttpContextAccessor httpContextAccessor, IJwtHelperAppService jwtHelperAppService = null)
        {
            this.httpContextAccessor = httpContextAccessor;
            _jwtHelperAppService = jwtHelperAppService;
        }
        public User GetUser()
        {
            string token = httpContextAccessor.HttpContext.Request.Headers["LUserInfo"].ToString();
            if (string.IsNullOrEmpty(token) )
            {
                return new User();
            }
            var user = _jwtHelperAppService.ParseJwt(token);
            if (user == null)
            {
                return new User();
            }
            return user;
        }
    }
}
